package com.example.fengyungo_user.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo_user.R;
import com.example.fengyungo_user.activity.BaseActivity;
import com.example.fengyungo_user.adapter.SelectDriverAdapter;
import com.example.fengyungo_user.constant.OrderConstant;
import com.example.fengyungo_user.constant.ResultConstant;
import com.example.fengyungo_user.constant.UserConstant;
import com.example.fengyungo_user.domain.DriverWithNumber;
import com.example.fengyungo_user.utils.OkHttpUtils;
import com.example.fengyungo_user.utils.ThreadPoolUtil;

import java.util.List;

public class SelectDriverActivity extends BaseActivity {

    private ImageButton backBtn;

    private RecyclerView recyclerView;

    private ProgressBar progressBar;
    /**
     * 订单编号
     */
    private String number;
    /**
     * 起点
     */
    private String begin;
    /**
     * 终点
     */
    private String end;

    private SwipeRefreshLayout swipeRefreshLayout;

    private LinearLayoutManager layoutManager;

    private SelectDriverAdapter adapter;

    private List<DriverWithNumber> drivers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_driver);

        recyclerView = findViewById(R.id.recyclerview);
        progressBar = findViewById(R.id.progress_bar);

        layoutManager = new LinearLayoutManager(SelectDriverActivity.this);

        // 获取number-订单编号
        Intent intent = getIntent();
        number = intent.getStringExtra(OrderConstant.NUMBER);
        begin = intent.getStringExtra(OrderConstant.BEGIN);
        end = intent.getStringExtra(OrderConstant.END);

        // 返回
        backBtn = findViewById(R.id.back);
        backBtn.setOnClickListener((v) -> finish());

        // 查询司机
        getDriversByNumber();

        // 下拉刷新
        swipeRefreshLayout = findViewById(R.id.refresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.soilyellow);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDriversByNumber();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    /**
     * 查询接单司机
     */
    private void getDriversByNumber() {
        Runnable runnable = () -> {
            try {
                // 发送GET请求到后端，并接收响应数据
                String url = UserConstant.URL_USER_USER + "/getDriversByOrderNumber/" + number;
                String responseData = OkHttpUtils.get(url);

                JSONObject jsonObject = JSONObject.parseObject(responseData);
                String data = jsonObject.getString(ResultConstant.DATA);
                drivers = JSON.parseArray(data, DriverWithNumber.class);

                for (DriverWithNumber driver : drivers) {
                    driver.setNumber(number);
                    driver.setBegin(begin);
                    driver.setEnd(end);
                }

                runOnUiThread(() -> {
                    progressBar.setVisibility(View.GONE);
                    recyclerView.setLayoutManager(layoutManager);
                    adapter = new SelectDriverAdapter(drivers);
                    recyclerView.setAdapter(adapter);
                });

            } catch (Exception e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(SelectDriverActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

        ThreadPoolUtil.execute(runnable);
    }
}